package com.ran.servlets;

import com.ran.service.BookService;
import com.ran.service.IBookServcie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/delete_book")
public class DeleteBook extends HttpServlet {

    private IBookServcie bookServcie;
    @Override
    public void init() throws ServletException {
        bookServcie = new BookService();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");

        // 如果需要支持特定的 HTTP 方法（如 POST, PUT, DELETE），可以添加以下头
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        String number = request.getParameter("number");
        response.setContentType("application/json;charset=UTF-8");
        if(number == null ||number.trim().isEmpty()){
             response.getWriter().write("{\"status\":\"error\"}");

        }else{
            boolean flag = bookServcie.delete(number);

            if (flag){
                response.getWriter().write("{\"status\":\"success\"}");
            }else {
                response.getWriter().write("{\"status\":\"fail\"}");
            }
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }




}